package com.pshcp.patient.api;

import com.external.common.WebServiceException;
import com.external.service.DongHuaWsService;
import com.external.ws.donghua.res.data.Document;
import com.external.ws.donghua.res.data.DocumentSearchData;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.tool.CommonTool;
import com.pshcp.domain.model.Patient;
import com.pshcp.patient.domain.searchreport.ao.ReportDetailAO;
import com.pshcp.patient.domain.searchreport.ao.ReportListAO;
import com.pshcp.patient.domain.searchreport.vo.ReportDetailVO;
import com.pshcp.patient.domain.searchreport.vo.ReportListVO;
import com.pshcp.patient.tool.SearchReportTools;
import com.pshcp.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = {"患者端-查询-报告查询"})
@Slf4j
@RestController
@RequestMapping("/api/patient/searchReport")
public class SearchReportAPI {

    @Autowired
    private DongHuaWsService dongHuaWsService;

    @Autowired
    private PatientService patientService;

    /**
     * 使用测试数据
     */
    @Value("${ws.test:false}")
    private Boolean hisTest;

    @ApiOperation("报告记录")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "报告记录")
    @PostMapping("/reportList")
    public ResultDTO<List<ReportListVO>> reportList(@RequestBody ReportListAO listAO) throws WebServiceException {
        Patient patient = patientService.findById(listAO.getPatientId());
        List<ReportListVO> list = null;
        try{
            List<Document> documents = dongHuaWsService.documentRetrieval(SearchReportTools.buildWsRetrievalDTO(listAO, patient));
            list = CommonTool.copyPropertiesList(documents, ReportListVO.class);
        }catch (Exception e){
            log.error("documentRetrieval error", e);
        }

        if(hisTest){
            list = SearchReportTools.buildTestData(listAO.getType());
        }
        return ResultBuilder.success(list);
    }

    @ApiOperation("检查/检验报告详情")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "检查/检验报告详情")
    @PostMapping("/reportDetail")
    public ResultDTO<ReportDetailVO> reportDetail(@RequestBody ReportDetailAO detailAO) throws WebServiceException {
        Patient patient = patientService.findById(detailAO.getPatientId());
        ReportDetailVO detailVO = null;
        try {
            List<DocumentSearchData> searchData = dongHuaWsService.documentSearch(SearchReportTools.buildWsSearchDTO(detailAO, patient));
            detailVO = CollectionUtils.isEmpty(searchData) ?
                    null : CommonTool.copyProperties(searchData.get(0), ReportDetailVO.class);
        }catch (Exception e){
            log.error("documentSearch ws error", e);
        }
        if(hisTest){
            detailVO = SearchReportTools.buildTestDataDetail();
        }
        return ResultBuilder.success(detailVO);
    }

}
